﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxTabControl;

namespace Project_Udrinkwater.Empoyee_Management
{
    public partial class SalePerDay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }



        protected void LinkButton2_Command(object sender, CommandEventArgs e)
        {
            string OrderID = e.CommandArgument.ToString();
            Session["MOrder"] = OrderID;
            DetailGrid.DataBind();
            Panel2.Visible = false;
            Panel3.Visible = true;
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
            Panel3.Visible = false;
            Session["MOrder"] = null;

        }

        protected void ASPxButton2_Click(object sender, EventArgs e)
        {
            MainGrid.AddNewRow();
        }

        protected void ASPxButton3_Click(object sender, EventArgs e)
        {
            DetailGrid.AddNewRow();
        }

        protected void cmbProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            ASPxComboBox combo1 = (ASPxComboBox) sender;
            object oldCat1 = Session["sProduct_ID"];
            if (oldCat1 != null && oldCat1.Equals(combo1.Value)) return;
            Session["sProduct_ID"] = combo1.Value;
            ASPxComboBox combo2 = ((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbOrdercate"));
            combo2.Value = null;

            cmbOrdercate_SelectedIndexChanged(combo2, EventArgs.Empty);
        }

        protected void cmbOrdercate_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            ASPxComboBox combo2 = (ASPxComboBox) sender;
            object oldCat2 = Session["sOrderCatID"];
            if (oldCat2 != null && oldCat2.Equals(combo2.Value)) return;
            Session["sOrderCatID"] = combo2.Value;
            //ASPxComboBox combo3 = ((ASPxComboBox)DetailGrid.FindEditRowCellTemplateControl(
            //    DetailGrid.Columns["Price_Unit"] as GridViewDataComboBoxColumn, "cmbPrice"));
            ASPxComboBox combo3 = ((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbPrice"));
            combo3.Value = null;

        }

        protected void DetailGrid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            
            string aa = e.OldValues["Discount"].ToString();

            e.NewValues["Product_ID"] = ((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbProduct")).Value;
            e.NewValues["Order_Category_ID"] =((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbOrdercate")).Value;
            e.NewValues["Price_Unit"] = ((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbPrice")).Value;
            e.NewValues["Amount"] = ((ASPxTextBox) DetailGrid.FindEditFormTemplateControl("txtAmount")).Text;
            e.NewValues["Discount"] = ((ASPxTextBox) DetailGrid.FindEditFormTemplateControl("txtDiscount")).Text;
        }

        protected void DetailGrid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            

            e.NewValues["Product_ID"] = ((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbProduct")).Value;
            e.NewValues["Order_Category_ID"] =((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbOrdercate")).Value;
            e.NewValues["Price_Unit"] = ((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbPrice")).Value;
            e.NewValues["Amount"] = ((ASPxTextBox) DetailGrid.FindEditFormTemplateControl("txtAmount")).Text;
            e.NewValues["Discount"] = ((ASPxTextBox) DetailGrid.FindEditFormTemplateControl("txtDiscount")).Text;
        }

        protected void DetailGrid_HtmlEditFormCreated(object sender, ASPxGridViewEditFormEventArgs e)
        { 
          

            ASPxComboBox combo2 =((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbProduct") as ASPxComboBox);
            if (Request.Params[combo2.UniqueID] != null) return;
            if (!DetailGrid.IsNewRowEditing)
            {
                object[] vals = (object[]) DetailGrid.GetRowValues(DetailGrid.EditingRowVisibleIndex,
                                                                   new string[]
                                                                       {
                                                                           "Product_ID", "Order_Category_ID",
                                                                           "Price_Unit","Amount","Discount"
                                                                       });
                
                ASPxComboBox combo3 =((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbOrdercate") as ASPxComboBox);
                ASPxComboBox combo4 =((ASPxComboBox) DetailGrid.FindEditFormTemplateControl("cmbPrice") as ASPxComboBox);
                ASPxTextBox txt1 = ((ASPxTextBox) DetailGrid.FindEditFormTemplateControl("txtAmount")) as ASPxTextBox;
                ASPxTextBox txt2 = ((ASPxTextBox) DetailGrid.FindEditFormTemplateControl("txtDiscount")) as ASPxTextBox;
                combo2.Value = vals[0];
                combo3.Value = vals[1];
                combo4.Value = vals[2];
                txt1.Text = vals[3].ToString();
                txt2.Text = vals[4].ToString();
                
            }
            


        }

       
        


}
}